package logic

import (
	"context"
	"fmt"

	"mall/service/attachment/rpc/attachmentclient"
	"mall/service/mqueue/job/internal/svc"

	"github.com/hibiken/asynq"
	// "github.com/sirupsen/logrus"
	"github.com/zeromicro/go-zero/core/logx"
)

type ScanAttachmentLogProcessor struct {
	svcCtx *svc.ServiceContext
}

func NewScanAttachmentLogProcessor(svcCtx *svc.ServiceContext) *ScanAttachmentLogProcessor {
	return &ScanAttachmentLogProcessor{
		svcCtx: svcCtx,
	}
}

// every five minutes exec : if return err != nil , asynq will retry
func (l *ScanAttachmentLogProcessor) ProcessTask(ctx context.Context, t *asynq.Task) error {
	// logrus.Info(fmt.Sprintf("ScanAttachmentLog Task: %+v", t))
	// &{typename:schedule:scan:attachmentLog payload:[] opts:[] w:0xc0007b1480}

	_, err := l.svcCtx.AttachmentRpc.ScanAttachmentLog(ctx, &attachmentclient.ScanLogReq{
		PageSize: uint64(50),
	})
	if err != nil {
		logx.Errorw("ScanAttachmentLogProcessor err", logx.Field("detail", err.Error()))
		return fmt.Errorf("ScanAttachmentLogProcessor err: %+v: %w ", err, asynq.SkipRetry)
	}

	return nil
}
